package com.example.tianqi.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.LinearLayoutCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tianqi.R;
import com.example.tianqi.adapter.CityAdapter;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;


public class CityActivity extends AppCompatActivity {

    private AppCompatImageView backImageView;
    private AppCompatImageView cityImageView;
    private RecyclerView mainRecyclerView;

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private ArrayList<String> cityArrayList = new ArrayList<>();
    private CityAdapter cityAdapter = new CityAdapter(cityArrayList);

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);

        backImageView = findViewById(R.id.backImageView);
        cityImageView = findViewById(R.id.cityImageView);
        mainRecyclerView = findViewById(R.id.mainRecyclerView);

        sharedPreferences = getSharedPreferences("weather", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        String cityJson = sharedPreferences.getString("city_json", "");
        try {
            JSONArray jsonArray = new JSONArray(cityJson);
            for (int i = 0; i < jsonArray.length(); i++) {
                cityArrayList.add(jsonArray.getString(i));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mainRecyclerView.setLayoutManager(linearLayoutManager);
        mainRecyclerView.setAdapter(cityAdapter);

        cityImageView.setOnClickListener(view -> {
            Intent intent = new Intent(CityActivity.this, CityAddActivity.class);
            startActivityForResult(intent, 1);
        });

        backImageView.setOnClickListener(view -> finish());

        cityAdapter.setOnItemClickListener(new CityAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position, String bean) {
                Intent intent = new Intent();
                intent.putExtra("city", bean);
                setResult(RESULT_OK, intent);
                finish();
            }

            @Override
            public void onClickDelete(int position, String bean) {
                cityArrayList.remove(position);
                cityAdapter.notifyItemRemoved(position);
                editor.putString("city_json", new Gson().toJson(cityArrayList));
                editor.apply();
            }
        });

    }

    @Override
    protected void onActivityResult(int req, int res, @Nullable Intent data) {
        super.onActivityResult(req, res, data);
        if (res == RESULT_OK && req == 1 && data != null) {
            String city = data.getStringExtra("city");
            //去重重复的城市
            cityArrayList.add(city);
            cityAdapter.notifyDataSetChanged();
            editor.putString("city_json", new Gson().toJson(cityArrayList));
            editor.apply();
        }
    }

}
